package me.javaweb.seata.account.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.javaweb.seata.account.entity.Test;
import me.javaweb.seata.account.mapper.TestMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author jjh
 */
@Slf4j
@RestController
@AllArgsConstructor
public class TestController {

    private final TestMapper mapper;
    private final HttpServletRequest request;

    @PostMapping
    public Object save(Boolean flag) {
        Test test = new Test(request.getRemoteAddr());
        mapper.insert(test);
        if (flag) {
            log.info("account 服务插入失败。");
            throw new RuntimeException();
        }
        log.info("account 服务插入完成。");
        return test;
    }

    @GetMapping
    public List<Test> list() {
        List<Test> tests = mapper.selectList(Wrappers.<Test>lambdaQuery().orderByDesc(Test::getDate));
        return tests;
    }
}
